package com.example.weather.util;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;



public class NetUtil2 {
    public static final String URL_WEATHER_WITH_SIMP="https://apis.juhe.cn/simpleWeather/life?city=%E6%B8%A9%E5%B7%9E&key=5a2ee564e3a4710d862ef89ff575800a";
    public static String doGet2(String urlStr2){
        String result2="";
        HttpURLConnection connection2=null;
        InputStreamReader inputStreamReader2=null;
        BufferedReader bufferedReader2=null;
        try {
            URL url=new URL(urlStr2);
            connection2 = (HttpURLConnection) url.openConnection();
            connection2.setRequestMethod("GET");
            connection2.setConnectTimeout(5000);

            InputStream inputStream2 = connection2.getInputStream();
            inputStreamReader2=new InputStreamReader(inputStream2);

            bufferedReader2=new BufferedReader(inputStreamReader2);

            StringBuilder stringBuilder2=new StringBuilder();
            String line2="";
            while ((line2=bufferedReader2.readLine())!=null){
                stringBuilder2.append(line2);
            }
            result2=stringBuilder2.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (connection2!=null){
                connection2.disconnect();
            }

            if (inputStreamReader2!=null){
                try {
                    inputStreamReader2.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }

            if (bufferedReader2!=null){
                try {
                    bufferedReader2.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return result2;
    }
    public static String getWeatherSimp(String city){
        //获取天气URL
        String weatherUrl2=URL_WEATHER_WITH_SIMP+"&city="+city;
        Log.d("fan","----weatherUrl2222----"+weatherUrl2);
        String weatherResult2=doGet2(weatherUrl2);
        Log.d("fan","----weatherResult2222----"+weatherResult2);
        return weatherResult2;
    }
}
